package com.hjm.common.utils.excel;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.sax.Excel03SaxReader;
import cn.hutool.poi.excel.sax.handler.RowHandler;
import lombok.Data;

import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.*;

/**
 * @ClassName ReadExcelUtils
 * @Deacription TODO
 * @Author Gd
 * @Version 1.0
 **/

@Data
public class ReadExcelUtils<T> {

    private static StringBuilder builder = new StringBuilder("key");    //结果集map key
    private Excel03SaxReader excel03SaxReader;                          //excel工具类型属性
    private String path;                                                //路径
    private File file;                                                  //文件
    private int sheetIndex;                                             //sheet下标
    private List<Object> data;                                          //读取到的原生数据
    private List<Map<String,String>> list = Collections.synchronizedList(new ArrayList<>());                    //读取到的数据
    private Map<String,List<Map<String,String>>> sheetMap = Collections.synchronizedMap(new HashMap<>());        //读取到的多sheet




    public <T> Object classFormatEntity(Class class1){
        Field[] declaredFields = class1.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField.getName());
        }
        return null;
    }


    /**
     * 拼接字符串  格式为  key0
     * @Param [str]
     * @Return java.lang.String
     */
    public String getStr(Object index){
        if (ObjectUtil.isNull(index)) {
            index = "";
        }
        return new StringBuilder("key").append(index).toString();
    }


    /**
     * @Param [path 根据一个路径去读取文件]
     * @Return
     */
    public ReadExcelUtils(String path){
        excel03SaxReader = new Excel03SaxReader(createRowHandler());
        excel03SaxReader.read(path,0);
    }
    public ReadExcelUtils(InputStream is){
        excel03SaxReader = new Excel03SaxReader(createRowHandler());
        excel03SaxReader.read(is,0);
    }


   /**
    *
    * @Param [file 根据一个File路径去读取文件]
    * @Return
    */
   public ReadExcelUtils(File file){
        excel03SaxReader = new Excel03SaxReader(createRowHandler());
        excel03SaxReader.read(path,0);
    }

    /**
     *
     * @Param [file 根据一个File路径去读取文件, sheetIndex-指定一个sheet下标]
     * @Return
     */
    public ReadExcelUtils(File file, int sheetIndex){
        excel03SaxReader = new Excel03SaxReader(createRowHandler());
        excel03SaxReader.read(path,sheetIndex);
    }

    /**
     * 获取多个sheet内容
     * @Param [file, sheetIndex]
     * @Return
     */
    public ReadExcelUtils(File file, int... sheetIndex){
        excel03SaxReader = new Excel03SaxReader(createRowHandler());
       for (int index : sheetIndex) {
           excel03SaxReader.read(file,index);
           sheetMap.put("s"+index,this.getList());
       }
    }


    /**
     * 读取一个sheet数据
     */
    private  RowHandler createRowHandler() {
        return (sheetIndex, rowIndex, rowlist) -> {
            Map<String,String> row = new HashMap<>();
            row.put(getStr(rowIndex),rowlist.get((int)rowIndex) == null ? "" : rowlist.get((int)rowIndex).toString());
            list.add(row);
        };
    }


    public static void main(String[] args) {
        ReadExcelUtils excelUtils = new ReadExcelUtils(new File("d:/88.xls"),new int[]{0,1,2});
      /*  List<Object> data = excelUtils.getData();
        System.out.println(data);
        List<Map<String, String>> list = excelUtils.getList();
        System.out.println(list);*/

        System.out.println(excelUtils.getSheetMap());

        /*for (int i = 0; i < 1000; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    ReadExcelUtils excelUtils = new ReadExcelUtils("d:/77.xls");
                    System.out.println("线程名称" + Thread.currentThread().getName() + excelUtils);
                    if (threadName.contains(Thread.currentThread().getName())) {
                        return;
                    } else {
                        threadName.add(Thread.currentThread().getName());
                    }
                }
            }).start();
        }*/




    }



}
